package com.bootdo.appsystem.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.appsystem.domain.ShouruDO;
import com.bootdo.appsystem.service.ShouruService;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.MD5Utils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.appsystem.service.UseraccountService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @author xiaoxiao
 * @email 806191255@qq.com
 * @date 2018-05-12 14:42:14
 */
 
@Controller
@RequestMapping("/appsystem/useraccount")
public class UseraccountController {
	@Autowired
	private UseraccountService useraccountService;
	@Autowired
    private ShouruService shouruService;
	
	@GetMapping()
	@RequiresPermissions("appsystem:useraccount:useraccount")
	String Useraccount(){
	    return "appsystem/useraccount/useraccount";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("appsystem:useraccount:useraccount")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UseraccountDO> useraccountList = useraccountService.list(query);
		int total = useraccountService.count(query);
		PageUtils pageUtils = new PageUtils(useraccountList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("appsystem:useraccount:add")
	String add(){
	    return "appsystem/useraccount/add";
	}

	@GetMapping("/edit/{uid}")
	@RequiresPermissions("appsystem:useraccount:edit")
	String edit(@PathVariable("uid") Integer uid,Model model){
		UseraccountDO useraccount = useraccountService.get(uid);
		model.addAttribute("useraccount", useraccount);
	    return "appsystem/useraccount/edit";
	}

    /**
     * 修改重要信息
     * @param uid
     * @param model
     * @return
     */
	@Log("【敏感操作】修改用户重要信息")
	@GetMapping("/editplus/{uid}")
	@RequiresPermissions("appsystem:useraccount:editplus")
	String editplus(@PathVariable("uid") Integer uid,Model model){
		UseraccountDO useraccount = useraccountService.get(uid);
		model.addAttribute("useraccount", useraccount);
		return "appsystem/useraccount/editplus";
	}


    /**
     * 给用户加钱
     * @return
     */
    @GetMapping("/toApplyMoney/{uid}")
    @RequiresPermissions("appsystem:useraccount:toApplyMoney")
	public String toApplyMoney(@PathVariable("uid") Integer uid,Model model){
        UseraccountDO useraccount = useraccountService.get(uid);
        model.addAttribute("useraccount", useraccount);
        return "appsystem/useraccount/applymoney";
    }

    /**
     * 给用户加钱
     */
	@Log("【敏感操作】修改用户余额")
    @ResponseBody
    @RequestMapping("/applymoney")
    @RequiresPermissions("appsystem:useraccount:toApplyMoney")
    public R applymoney( Integer uid , String resource , Double money){
        //给用户加钱
        useraccountService.addMoney(uid,money);

        //增加收入信息
        ShouruDO shouru = new ShouruDO();
        shouru.setUid(uid);
        shouru.setDdcode("20000006");
        shouru.setResource(resource);
        shouru.setSrMoney(money);
        shouru.setSrdate(new Date());
        shouru.setType(resource);
        shouru.setShourucode(7);
        shouruService.save(shouru);

        return R.ok();
    }


	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("appsystem:useraccount:add")
	public R save( UseraccountDO useraccount){
		if(useraccountService.save(useraccount)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("修改用户信息")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("appsystem:useraccount:edit")
	public R update( UseraccountDO useraccount){
        String payword = useraccount.getPayword();
        if (payword != null && payword != "") {
            payword = MD5Utils.encrypt(AppUtilXML.SCOKED_NUMBER,payword);
            useraccount.setPayword(payword);
        }
        String password = useraccount.getPassword();
        if (password != null && password != "") {
            password = MD5Utils.encrypt(AppUtilXML.SCOKED_NUMBER,password);
            useraccount.setPassword(password);
        }

        useraccountService.update(useraccount);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("appsystem:useraccount:remove")
	public R remove( Integer uid){
		if(useraccountService.remove(uid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("appsystem:useraccount:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] uids){
		useraccountService.batchRemove(uids);
		return R.ok();
	}
	
}
